package common;
import java.sql.SQLException;
import java.util.ArrayList;

public class RecommendationGenerator {

    
    private ArrayList<Recommendation> recommendations;
    private DataManager dm;
    private static double PERCENTAGE = 1.10;
    
    
    public RecommendationGenerator(){
        
        recommendations = new ArrayList<Recommendation>();
        dm = new DataManager();
    }
    
    public void makeRecommendations() throws SQLException {
        ArrayList<Integer> users;
        int average;
        ArrayList<Integer> weekdayHourlyAverage, weekendHourlyAverage;

        //Delete current set of recommendations from database
        dm.clearRecs();
            
        average = dm.getOverallAverage();
        
        weekdayHourlyAverage = dm.getHourlyAverage(true);
        weekendHourlyAverage = dm.getHourlyAverage(false);
        //assuming we get from the database an arraylist of data per user
        //assuming it contains 24 pieces of info since it's an average
        //usage day either week or weekend
        ArrayList<SensorData> tempData = new ArrayList<SensorData>();
        
        users = dm.getUsers();
        ArrayList<ArrayList<SensorData>> userWeekData = new ArrayList<ArrayList<SensorData>>();
        ArrayList<ArrayList<SensorData>> userWeekendData = new ArrayList<ArrayList<SensorData>>();
        
        for(Integer user:users){
            tempData = dm.getWeekdayData(user);
            userWeekData.add(tempData);
            tempData = dm.getWeekendData(user);
            userWeekendData.add(tempData);
        }



        // * find the usage peaks in the neighborhood weekday
        for(int i  = 0;i < weekdayHourlyAverage.size();i++){
            if(weekdayHourlyAverage.get(i) > average){
                for(int j = 0; j< users.size();j++){
                    //check for all the users at that time whether the usage is more than the average
                    if(userWeekData.get(j).get(i).getWattage() > average){
                        Recommendation tempRec = new Recommendation();
                        String tempString = "You are using more than the average neighborhood electricity consumption at ";
                        tempString = tempString + i + " on weekdays because of appliances tagged as: ";
                        
                        ArrayList<String> tempTags;
                    //if so recommend to decrease usage at that time if possible
                        tempTags = userWeekData.get(j).get(i).getTags();
                        for(String tag:tempTags){
                            tempString = tempString +tag + ", ";
                        }
                        tempString = tempString + "please decrease your energy consumption.";
                    //if so  check whether it's next to a minimum and recommend to spread
                        if((i-1)>=0 && weekdayHourlyAverage.get(i-1) < average){
                            //spread earlier
                            tempString = tempString + "You can spread your usage to " +  (i-1) + ", an hour earlier";
                        }
                        if((i+1)<=23 && weekdayHourlyAverage.get(i+1) < average){
                            //spread later
                            tempString = tempString + "You can spread your usage to " + (i+1) + ", an hour later";
                        }
                        tempRec.setMessage(tempString);
                        tempRec.setId(users.get(j));
                        recommendations.add(tempRec);
                    }
                    
                }
            }
        }
        //for weekends
        for(int i  = 0;i < weekendHourlyAverage.size();i++){
            if(weekendHourlyAverage.get(i) > average){
                for(int j = 0; j< users.size();j++){
                    //check for all the users at that time whether the usage is more than the average
                    if(userWeekendData.get(j).get(i).getWattage() > average){
                        Recommendation tempRec = new Recommendation();
                        String tempString = "You are using more than the average neighborhood electricity consumption at ";
                        tempString = tempString + i + " on weekends because of appliances tagged as: ";
                        
                        ArrayList<String> tempTags;
                    //if so recommend to decrease usage at that time if possible
                        tempTags = userWeekendData.get(j).get(i).getTags();
                        for(String tag:tempTags){
                            tempString = tempString +tag + ", ";
                        }
                        tempString = tempString + "please decrease your energy consumption.";
                    //if so  check whether it's next to a minimum and recommend to spread
                        if((i-1)>=0 && weekendHourlyAverage.get(i-1) < average){
                            //spread earlier
                            tempString = tempString + "You can spread your usage to " +  (i-1) + ", an hour earlier";
                        }
                        if((i+1)<=23 && weekendHourlyAverage.get(i+1) < average){
                            //spread later
                            tempString = tempString + "You can spread your usage to " + (i+1) + ", an hour later";
                        }
                        tempRec.setMessage(tempString);
                        tempRec.setId(users.get(j));
                        recommendations.add(tempRec);
                    }
                    
                }
            }
        }
        
        for(Recommendation rec:recommendations){
            dm.makeRecommendation(rec);
        }
        
    }
}
